//
//  PictorialView.m
//  Pictorial
//
//  Created by SKY on 15/12/29.
//  Copyright © 2015年 sky. All rights reserved.
//

#import "PictorialView.h"
#import "PictorialCell.h"

#import "Net.h"
#import "ModelTool.h"

#import "PictorialModel.h"

@interface PictorialView ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate,PictorialCellDelegate>
{
    int _page;
    int _pageCount;
}
@property (nonatomic,strong) NSMutableArray *allData;
@property (nonatomic,strong) NSMutableArray *pageArray;
@property (nonatomic,strong) NSMutableArray *dataArray;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation PictorialView

-(void)willMoveToSuperview:(UIView *)newSuperview{
    self.frame = newSuperview.bounds;
    
    _page = 0;
    [self.pageArray addObject:[NSString stringWithFormat:@"%d",_page]];
    
    [self createUI];
    [self downloadData];
}
-(void)downloadData{
    
    [Net getRequestHttpURL:PICTORIAL_URL view:self completation:^(id object) {
        NSArray *array = object;
        for (NSDictionary *dict in array) {
//            [ModelTool createModelWithDictionary:dict];
            PictorialModel *model = [[PictorialModel alloc]init];
            model.destination = dict[@"destination"];
            
            NSMutableArray *articleArr = [NSMutableArray array];
            NSArray *arr = dict[@"articles"];
            for (NSDictionary *subDict in arr) {
//                [ModelTool createModelWithDictionary:subDict];
                PictorialArticleModel *model = [PictorialArticleModel objectWithDict:subDict];
                [articleArr addObject:model];
            }
            model.articles = articleArr;
            
            [self.allData addObject:model];
        }
        [self reloadData:_page];
    }];
}
- (void)reloadData:(int)page {
    
    if (page * _pageCount > self.allData.count) {
        return;
    }
    
    int count = (page + 1) * _pageCount > self.allData.count ? (self.allData.count % _pageCount):(_pageCount);
    for (int index = page * _pageCount; index < count + page * _pageCount; index ++) {
        [self.dataArray addObject:self.allData[index]];
    }
    
    [self.tableView reloadData];
    
}
- (IBAction)back:(id)sender {
    [_delegate back];
}
-(void)createUI{

    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    _pageCount = self.tableView.frame.size.height / 200 + 1;
}
-(NSMutableArray *)allData{
    if (_allData == nil) {
        _allData = [NSMutableArray array];
    }
    return _allData;
}
-(NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
-(NSMutableArray *)pageArray{
    if (_pageArray == nil) {
        _pageArray = [NSMutableArray array];
    }
    return _pageArray;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.y >= scrollView.contentSize.height - scrollView.frame.size.height) {
        _page ++;
        for (NSString *index in self.pageArray) {
            if ([index isEqualToString:([NSString stringWithFormat:@"%d",_page])]) {
                return;
            }
        }
        [self.pageArray addObject:[NSString stringWithFormat:@"%d",_page]];
        [self reloadData:_page];
    }
}
#pragma mark - PictorialCellDelegate
- (void)moveToDetailVC:(PictorialArticleModel *)model{
    [_delegate moveToPictorialDetailVC:model];
}
#pragma mark - TableView
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    PictorialCell *cell = [PictorialCell nibInitializtion];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.delegate = self;
    PictorialModel *model = self.dataArray[indexPath.row];
    cell.model = model;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 200;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

@end
